home *** CD-ROM | disk | FTP | other *** search
- /*---------------------------------------------------------------------------*\
- | UIWinPl.cpp - DevUI(tm) library window placement class |
- | From Developing User Interfaces for Microsoft Windows |
- | Copyright (c) 1999, Everett N. McKay |
- \*---------------------------------------------------------------------------*/
-
- #include "stdafx.h"
- #include "DevUI.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- // class CMcWindowPlacement
-
- // effect: Saves the window placement to registry.
- HRESULT CMcWindowPlacement::SetProfile (LPCTSTR section, LPCTSTR entry)
- {
- CWinApp *pApp = AfxGetApp();
- int retVal;
-
- ASSERT_VALID(pApp);
- ASSERT(section);
- ASSERT(entry);
-
- retVal = pApp->WriteProfileBinary(section, entry, (BYTE *)this, sizeof(WINDOWPLACEMENT));
- return (retVal > 0) ? NO_ERROR : E_FAIL;
- }
-
- // effect: Restores window placement from registry. If (checkValues), makes sure
- // size will fit on screen and location is on screen.
- HRESULT CMcWindowPlacement::GetProfile (LPCTSTR section, LPCTSTR entry, BOOL checkValues)
- {
- CWinApp *pApp = AfxGetApp();
- WINDOWPLACEMENT *pWp;
- UINT size;
-
- ASSERT_VALID(pApp);
- ASSERT(section);
- ASSERT(entry);
-
- if (!pApp->GetProfileBinary(section, entry, (BYTE **)&pWp, &size))
- return E_FAIL;
- if (size == 0)
- {
- // size will be 0 if reset - just return now
- delete pWp; // still must delete, since memory has been allocated
- return E_FAIL;
- }
- ASSERT(size == sizeof(WINDOWPLACEMENT));
- if (size != sizeof(WINDOWPLACEMENT))
- {
- delete pWp;
- return E_FAIL;
- }
-
- if (checkValues)
- {
- CRect tempRect, screenRect(0, 0, GetSystemMetrics(SM_CXSCREEN),
- GetSystemMetrics(SM_CYSCREEN));
-
- // make sure at least part of the window is visible
- if (!IntersectRect(&tempRect, &pWp->rcNormalPosition, &screenRect))
- {
- // move window to screen origin
- pWp->rcNormalPosition.right -= pWp->rcNormalPosition.left;
- pWp->rcNormalPosition.left = 0;
- pWp->rcNormalPosition.bottom -= pWp->rcNormalPosition.top;
- pWp->rcNormalPosition.top = 0;
- }
-
- // make window no larger than screen
- if (pWp->rcNormalPosition.right - pWp->rcNormalPosition.left > GetSystemMetrics(SM_CXSCREEN))
- pWp->rcNormalPosition.right = pWp->rcNormalPosition.left + GetSystemMetrics(SM_CXSCREEN);
- if (pWp->rcNormalPosition.bottom - pWp->rcNormalPosition.top > GetSystemMetrics(SM_CYSCREEN))
- pWp->rcNormalPosition.bottom = pWp->rcNormalPosition.top + GetSystemMetrics(SM_CYSCREEN);
- }
-
- memcpy(this, pWp, sizeof(WINDOWPLACEMENT));
- delete pWp;
- return NO_ERROR;
- }
-
- // effect: Save window placement for the given window, setting this to the given
- // window's placement.
- HRESULT CMcWindowPlacement::SaveWindowPlacement (LPCTSTR section, LPCTSTR entry,
- CWnd *pWnd)
- {
- ASSERT_VALID(pWnd);
-
- if (!pWnd->GetWindowPlacement(this))
- return E_FAIL;
- return SetProfile(section, entry);
- }
-
- // effect: Restores window placement for the given window, setting this to the
- // given window's placement. If (checkValues), makes sure
- // size will fit on screen and location is on screen.
- HRESULT CMcWindowPlacement::RestoreWindowPlacement (LPCTSTR section, LPCTSTR entry,
- CWnd *pWnd, BOOL checkValues)
- {
- ASSERT_VALID(pWnd);
-
- if (FAILED(GetProfile(section, entry, checkValues)))
- return E_FAIL;
-
- if (!pWnd->SetWindowPlacement(this))
- return E_FAIL;
- else
- return NO_ERROR;
- }
-